/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList1(ListNode head) {
        if(null == head) {
            return null;
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while(null!=cur) {
            stack.push(cur);
            cur = cur.next;
        }
        cur = head;
        ListNode ret = stack.peek();
        while(!stack.empty()) {
            cur = stack.pop(); 
            if(!stack.empty()) {
                cur.next = stack.peek();
            }
        }
        cur.next = null;
        return ret;
    }
    public ListNode reverseList(ListNode head) {
        if(null == head) {
            return null;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(null!=cur) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        head.next = null;
        return prev;
    }
}